package ru.amse.stroganova.graph;

/**
 * 
 * This class provides a simple implementation of a graph edge, applicable for
 * directed and undirected, weighted and unweighted graphs.
 * 
 * @author Dasha Stroganova
 * 
 */
public class Edge {

	private final Vertex source;

	private final Vertex destination;
	
	private int weight;

	/**
	 * Creates an edge between two vertices with default weight. Both vertices must be non-null.
	 * 
	 * @param source
	 *            source VertexImpl
	 * @param destination
	 *            destination vertex
	 */
	public Edge(Vertex source, Vertex destination) {
		this(source, destination, 0);
	}

	/**
	 * Creates an edge between two vertices with the given weight. Both vertices
	 * must be non-null.
	 * 
	 * @param source
	 *            source vertex
	 * @param destination
	 *            destination vertex
	 * @param weight
	 *            edge weight
	 */
	public Edge(Vertex source, Vertex destination,
			int weight) {
		if (source == null || destination == null) {
			throw new IllegalArgumentException("vertex is null");
		}
		this.source = source;
		this.destination = destination;
		this.weight = weight;
	}

	
	/**
	 * Returns a destination vertex of this edge.
	 * 
	 * @return destination vertex
	 */
	public Vertex getDestination() {
		return destination;
	}

	/**
	 * Returns a source vertex of this edge.
	 * 
	 * @return source vertex
	 */
	public Vertex getSource() {
		return source;
	}

	/**
	 * Returns a vertex opposite to the given one.
	 * 
	 * @param vertex
	 *            vertex
	 * @return opposite vertex; null if this edge is not incident to the
	 *         given vertex
	 */
	public Vertex getOppositeVertex(Vertex vertex) {
		return vertex.equals(source) ? destination : (vertex
				.equals(destination) ? source : null);
	}

	/**
	 * Returns the weight of this edge.
	 * 
	 * @return edge weight
	 */
	public int getWeight() {
		return weight;
	}

	/**
	 * Sets the weight of this edge.
	 * 
	 * @param weight
	 *            edge weight
	 */
	public void setWeight(int weight) {
		this.weight = weight;
	}
}
